package com.heima.behavior.service.impl;


import com.heima.behavior.pojos.UserLikeBehavior;
import com.heima.behavior.pojos.UserLoveBehavior;
import com.heima.behavior.service.UserBehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.behavior.dtos.UserLoveDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.BehaviorThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserBehaviorServiceImpl implements UserBehaviorService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 用户点赞
     * @param userLikeBehavior
     * @return
     */
    @Override
    public ResponseResult likeBehavior(UserLikeBehavior userLikeBehavior) {
        //1.参数校验
        if (userLikeBehavior==null || userLikeBehavior.getArticleId()==null || userLikeBehavior.getType()==null ||userLikeBehavior.getOperation()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.获取当登录用户的id
        Integer userId = BehaviorThreadLocalUtil.getUser().getId();
        //3.根据用户id查询是否存在对该文章的操作
        Query query = Query.query(Criteria.where("userId").is(userId).and("articleId").is(userLikeBehavior.getArticleId()));
        UserLikeBehavior likeBehavior = mongoTemplate.findOne(query, UserLikeBehavior.class);
        if (likeBehavior != null) {
            likeBehavior.setOperation(userLikeBehavior.getOperation());
            likeBehavior.setCreatedTime(new Date());
            mongoTemplate.save(likeBehavior);
            return ResponseResult.okResult("");
        }
        //不存在，封装数据保存
        likeBehavior = new UserLikeBehavior();
        likeBehavior.setUserId(userId);
        likeBehavior.setOperation(userLikeBehavior.getOperation());
        likeBehavior.setType(userLikeBehavior.getType());
        likeBehavior.setArticleId(userLikeBehavior.getArticleId());
        likeBehavior.setCreatedTime(new Date());

        mongoTemplate.save(likeBehavior);

        //将数据保存到redis中
        if (userLikeBehavior.getOperation()==0){
            redisTemplate.opsForHash().put(BehaviorConstants.BEHAVIOR_LIKE+likeBehavior.getArticleId(),
                    BehaviorConstants.BEHAVIOR_IS_LIKE+likeBehavior.getUserId(),
                    "1");
        }
        if (userLikeBehavior.getOperation()==1){
            redisTemplate.opsForHash().delete(BehaviorConstants.BEHAVIOR_LIKE+likeBehavior.getArticleId(),
                    BehaviorConstants.BEHAVIOR_IS_LIKE+likeBehavior.getUserId());
        }
        return ResponseResult.okResult("");
    }

    /**
     * 用户喜欢
     * @param userLoveDto
     * @return
     */
    @Override
    public ResponseResult loveBehavior(UserLoveDto userLoveDto) {
        //1.校验参数
        if (userLoveDto==null || userLoveDto.getArticleId()==null ||userLoveDto.getType()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.获取当前登录用户的id
        Integer userId = BehaviorThreadLocalUtil.getUser().getId();
        //3.查询该用户是否存在该文章的喜欢行为
        Query query = Query.query(Criteria.where("userId").is(userId).and("articleId").is(userLoveDto.getArticleId()));
        UserLoveBehavior loveBehavior = mongoTemplate.findOne(query, UserLoveBehavior.class);

        //3.判断是否存在数据信息
        if (loveBehavior != null){
            //存在喜欢数据
            loveBehavior.setType(userLoveDto.getType());
            loveBehavior.setCreatedTime(new Date());
            mongoTemplate.save(loveBehavior);
        }else {
            //不存在喜欢数据，就封装数据新增
            loveBehavior = new UserLoveBehavior();
            loveBehavior.setUserId(userId);
            loveBehavior.setArticleId(userLoveDto.getArticleId());
            loveBehavior.setType(userLoveDto.getType());
            loveBehavior.setCreatedTime(new Date());
            mongoTemplate.save(loveBehavior);
        }

        //存储到redis中
        if (userLoveDto.getType()==0){
            redisTemplate.opsForHash().put(BehaviorConstants.BEHAVIOR_LOVE+loveBehavior.getArticleId(),
                    BehaviorConstants.BEHAVIOR_IS_LOVE+loveBehavior.getUserId(),
                    "1");
        }
        if (userLoveDto.getType()==1){
            redisTemplate.opsForHash().delete(BehaviorConstants.BEHAVIOR_LOVE+loveBehavior.getArticleId(),
                    BehaviorConstants.BEHAVIOR_IS_LOVE+loveBehavior.getUserId());
        }
        return ResponseResult.okResult("");
    }

}
